<?php
namespace app\app\controller\v1;
use think\Db;
use think\Request;
use app\app\util\OnlineUtils;

class UserController extends BaseController{

    /**
     * 显示资源列表
     * 我的战报列表
     * @return \think\Response
     */
    public function index()
    {

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
    	
    }

    /**
     * 显示指定的资源
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

    }

    /**
     * [getUserInfo 获取个人信息]
     * @return [json] [description]
     */
    public function getUserInfo()
    {
    	$user_id = OnlineUtils::getUserIdBySid();
    	$user    = Db::name('user')->where('id','eq',$user_id)->find();

    	//用户所有（电脑、匹配的总游戏次数）
        $computer_times = Db::name('computer')->where('user_id','eq',$user_id)->count();
        $match_times = Db::name('match_fight')->where('user_id','eq',$user_id)->count();
        $user['times'] = $computer_times."+".$match_times;
        //用户获胜（result为1）次数
        $computer_victory_times = Db::name('computer')->where('user_id','eq',$user_id)->where('result','eq',1)->count();
        $match_victory_times = Db::name('match_fight')->where('user_id','eq',$user_id)->where('result','eq',1)->count();
        $user['victory_times'] = $computer_victory_times."+".$match_victory_times;

    	return ft(200,'success',$user);
    }


    /**
     * [myVictory 我的胜负战报]
     * @return [type] [description]
     */
    public function myVictory()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        //查询出 战报
        $victory = Db::query('select a.id, from ft_victory a left join ft_team b on a.tid=b.id where a.uid='.$user_id.' and b.status=1 order by a.id desc');
        if($victory){
            foreach($victory as $k => $v){
                if($v['result'] == 1){
                    $victory[$k]['result'] = "胜利";
                }elseif($v['result'] == 0){
                    $victory[$k]['result'] = "失败";
                }else{
                    $victory[$k]['result'] = "对战中";
                }
            }
        }else{
            $victory = [];
        }
        return ft(200,'success',['data'=>$victory]);
    }


    /**
     * [resetFreeNumber 定时任务,重置免费分享与投票次数]
     * @return [type] [description]
     */
    public function resetFreeNumber()
    {
        Db::query('update ft_user set free_share = 3,free_vote = 1 where status=1');
        return ft(200,'success');
    }

    /**
     * [message 获取access_token]
     * @return [type] [description]
     */
    private function getAccessToken()
    {
        //请求地址
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('APPID')."&secret=".config('SECRET');
        //数据
        $result = makeRequest($url,array());
        $result = json_decode($result);
        $access_token = $result->access_token;
        return $access_token;
    }

    /**
     * [message 消息通知]
     * @return [type] [description]
     */
    public function message()
    {
        //遍历数据库给所有前一天参与活动的用户都发送消息
        $user = Db::query('select a.id as uid,b.id,a.open_id,b.formid,b.vid from ft_user a left join ft_victory_messages b on a.id=b.uid where b.status=1');
        if($user){
            foreach($user as $k => $v){
                //access_token
                $access_token = $this->getAccessToken();
                //formid
                $formid = $v['formid'];
                //开奖结果
                $fight  = "昨天比赛的结果出来了，快来看看吧";
                $word   = "赢了";
                //发送的消息
                $data_arr = array(
                    'keyword1'=>array('value'=>'爱看世界杯'),
                    'keyword2'=>array('value'=>$fight)
                );
                //发送的数据
                $post_data = array(
                    "touser"          => $v['open_id'],
                    "template_id"     => config('MESSAGE_ID'),
                    "page"            => "pages/record/record",
                    "form_id"         => $formid,
                    "data"            => $data_arr,
                    "emphasis_keyword"=> $word
                );
                //请求的url
                $url    = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token;
                //发送post请求
                $params = json_encode($post_data);
                $result = makeRequest($url,$params);
            }
        }
        return ft(200,'success');
    }

    public function editVote()
    {
        $id = input('get.id')?input('get.id'):0;
        return ft(200,'success',['data'=>$id]);
    }


}